<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\index\model;
use think\Db;
use think\Model;

class ApiModel extends Model
{

    public function opend(){
      	
		$openlist=Db::name('opend')->order("id desc")->limit(2)->select()->toArray();
      	$qihao=$openlist[0]['term'];
      	//获取当前开奖结果
      	$kjnum=explode(",",$openlist[0]['num']);      	
      	$ss="";
      	//风控系统测试
      	//先检测大小单双的下注金额获取作弊条件
      	$zb_da=Db::name('orders')->where(['term'=>$qihao,'account'=>"s0"])->sum('money');
      	$zb_x=Db::name('orders')->where(['term'=>$qihao,'account'=>"s1"])->sum('money');
      	$zb_dan=Db::name('orders')->where(['term'=>$qihao,'account'=>"s2"])->sum('money');
      	$zb_s=Db::name('orders')->where(['term'=>$qihao,'account'=>"s3"])->sum('money');
      	if($kjnum[4]>=5){
        	$zb_dxis=1;
        }elseif($kjnum[4]<5){
        	$zb_dxis=0;
        }
      	if($kjnum[4]%2==0){
          	//双
        	$zb_dsis=1;
        }else{
        	$zb_dsis=0;
        }
		//大小总金额计算完毕开始计算龙虎下注总额
      	 $zb_l=Db::name('orders')->where(['term'=>$qihao,'account'=>"l0"])->sum('money');
      	 $zb_h=Db::name('orders')->where(['term'=>$qihao,'account'=>"l1"])->sum('money');
     	 $zb_he=Db::name('orders')->where(['term'=>$qihao,'account'=>"l2"])->sum('money');
      	if($kjnum[2]==$kjnum[4]){
        	$zb_kjlh=1;
        }elseif($kjnum[2]>$kjnum[4]){
        	$zb_kjlh=2;
        }elseif($kjnum[2]<$kjnum[4]){
        	$zb_kjlh=3;
        }
      	//计算牛牛下注总额
      	$zbnn_num=$kjnum[4]+1;
      	$zb="";
      	for($i=$zbnn_num;$i<=8;$i++){
        	$zb.=",n".$i;
        }
		$zb_nn_nnmoney=0;
      	$fengkong=Db::name('orders')->where(['term'=>$qihao,'state'=>0,'types'=>0,'account'=>array('in',$zb)])->select();
      	foreach($fengkong as $v){
        	$zb_nn_nnmoney+=$v['money'];
        }
      	//这里进行如何开奖的逻辑运算
          /*
          先比较三大分类那边下注比较多侧重与多的一边
          */
      	$zb_openlh=0;
      	$zb_dxds_sum=$zb_da+$zb_x+$zb_dan+$zb_s;
      	$zb_lh_sum=$zb_l+$zb_h+$zb_he;
      	$zb_nn_nnmoney=$zb_nn_nnmoney;
      	if($zb_dxds_sum>$zb_lh_sum){
          //如果大小下注大于龙虎
        	if($zb_nn_nnmoney>$zb_dxds_sum){
            	//如果牛牛大于大小
              	$zb_open=mt_rand(7,8);
            }else{
            	if($zb_da+$zb_x>$zb_dan+$zb_s){
                	//如果大小大于单双
                  	if($zb_dxis){
                    	$zb_open=mt_rand(1,4);
                    }else{
                    	$zb_open=mt_rand(5,9);
                    }	
                }else{
                	if($zb_dsis){
                    	 $zb_open=3;
                    }else{
                    	 $zb_open=6;
                    }
                }
            }
        }elseif($zb_nn_nnmoney>$zb_lh_sum){
          //如果牛牛大于龙虎
          $zb_open=mt_rand(7,8);
        }else{
        	$zb_l8=$zb_l*8;
          	$zb_h8=$zb_h*8;
          	if($zb_l8>=$zb_he || $zb_h8>=$zb_he){
            	//如果龙虎的下注有一个下注金额的8倍大于合的下注 那么开合
              	$zb_test_lh=mt_rand(1,6);
              	for($i=1;$i<=$zb_test_lh;$i++){
                	$zb_openlh=$i;
                	$zb_open=$i;
                }
            }else{
            	if($zb_l>=$zb_h){
                  	$zb_openlh=mt_rand(4,9);
                	$zb_open=mt_rand(1,3);
                }else{
                	$zb_openlh=mt_rand(1,4);
                	$zb_open=mt_rand(5,9);
                }
            }
        }
      	//这里更改开奖信息的部分	
      	//获取总充值和提现
      	$zb_sumtx=Db::name('withdraw')->where('status',1)->whereTime('addtime', 'd')->sum('money');
      	$zb_sumpay=Db::name('pay')->where('status',1)->whereTime('addtime', 'd')->sum('money');
      	$zb_qihao=$openlist[1]['zuobi'];
      	if($zb_sumtx>=$zb_sumpay+99999 && $zb_qihao==0){
          	if($zb_openlh){
            	$kjnum[2]=$zb_openlh;
              	$kjnum[4]=$zb_open;
            }else{
            	$kjnum[4]=$zb_open;
            }
          	$zuobi=1;
        }else{
        	$zuobi=0;
        }
      	if($zuobi==1){
          	$zb_info=$kjnum[0].",".$kjnum[1].",".$kjnum[2].",".$kjnum[3].",".$kjnum[4];
        	Db::name('opend')->where('term', $qihao)->update(['num' => $zb_info,'zuobi'=>1]);
        }
      	//风控结束
      	//获取特码进行大小单双计算
      	$newdxkj=$kjnum[2]+$kjnum[3]+$kjnum[4];
      	if($newdxkj>=0 && $newdxkj<=12){
        	$ss.=",x";
        }
      	if($newdxkj>=13 && $newdxkj<=14){
        	$ss.=",h";
        }
      	if($newdxkj>=15 && $newdxkj<=27){
        	$ss.=",d";
        }
      	//单双
      	if($newdxkj%2==0 && $newdxkj!=13 && $newdxkj!=14){
            $ss.=",s";
        }
      	if($newdxkj%2!=0 && $newdxkj!=13 && $newdxkj!=14){
            $ss.=",n";
        }
        //单码
      	$ss.=",".$kjnum[2];
      	$ss.=",".$kjnum[3];
      	$ss.=",".$kjnum[4];
      	
      	$dxkj=$kjnum[4];
      	if($dxkj>=5){
        	$ss.=",s0";
        }elseif($dxkj<5){
        	$ss.=",s1";
        }
      	if($dxkj%2==0){
        	$ss.=",s3";
        }elseif($dxkj%2!=0){
        	$ss.=",s2";
        }
      	//大单判断
      	if($dxkj%2!=0){
        	if($dxkj>=5){
            	$ss.=",b0";
            }	
        }
      	if($dxkj%2!=0){
        	if($dxkj<5){
            	$ss.=",b1";
            }	
        }
      	//双判断
      	if($dxkj%2==0){
        	if($dxkj>=5){
            	$ss.=",b2";
            }	
        }
      	if($dxkj%2==0){
        	if($dxkj<5){
            	$ss.=",b3";
            }	
        }
      	//开始龙虎结算
      	if($kjnum[2]==$kjnum[4]){
        	$ss.=",l2";
        }elseif($kjnum[2]>$kjnum[4]){
        	$ss.=",l1";
        }elseif($kjnum[2]<$kjnum[4]){
        	$ss.=",l0";
        }
      	//新版牛牛结算
      	$niuniunum=$kjnum[4]+1;
      	for($i=$niuniunum;$i<=8;$i++){
        	$ss.=",n".$i;
        }
      	//print_r($fengkong);
      	
      	//获取中奖的下注记录
      	$ss=substr($ss,1);
      	$test=explode(",",$ss);
      	$term=$openlist[0]['term'];
      	$jiesuan=Db::name('orders')->where(['term'=>$term,'state'=>0,'types'=>0,'account'=>array('in',$ss)])->select();
      	$ten=9.7;
      	$dmt=1.9;
      	$daxp=1.9;
      	//第一
      
      	$kjpl=array(
        		's0'=>$daxp,
		        's1'=>$daxp,
		        's2'=>$daxp,
		        's3'=>$daxp,
          		'd'=>2,
		        'x'=>2,
		        'h'=>3,
        );
      	if($kjnum[2]==$kjnum[3]){
          	if($kjnum[3]==$kjnum[4]){
            	$kjpl[$kjnum[2]]=2.5;
            }else{
            	 $kjpl[$kjnum[2]]=2.5;
            }
        }else{
            	$kjpl[$kjnum[2]]=2.5;
        }
      	//第二
      	if($kjnum[3]==$kjnum[4]){
          	if($kjnum[2]==$kjnum[4]){
            	$kjpl[$kjnum[3]]=2.5;
            }else{
            	$kjpl[$kjnum[3]]=2.5;
            }
        }else{
        	$kjpl[$kjnum[3]]=2.5;
        }
      	//第三
      	if($kjnum[4]==$kjnum[3]){
          	if($kjnum[4]==$kjnum[2]){
            	$kjpl[$kjnum[4]]=2.5;
            }else{
            	$kjpl[$kjnum[4]]=2.5;
            }
        }else{
        	$kjpl[$kjnum[4]]=2.5;
        }
      	$openlist=Db::name('orders')->where('term',$qihao)->select()->toArray();
      	foreach($jiesuan as $v){          	
          //获取结算赔率金额
        	$price=$v['money']*($kjpl[$v['account']]);
         	//修改订单
          	Db::name('orders')->where('id', $v['id']) ->update(['zj'=>$price]);
          	//增加用户余额
          	Db::name('user')->where('id', $v['user'])->setInc('balance',$price);
          	//家电齐纳
          	 Db::name('opend')->where('term', $term)->setInc('kjmoney',$price);
        }
      	foreach($openlist as $v){          	
          	//家电齐纳
          	 Db::name('opend')->where('term', $term)->setInc('money',$v['money']);
        }
      	$sljiesuan=Db::name('orders')->where(['term'=>$term,'state'=>0,'types'=>1])->select();
        
      	//结算进行完毕修改所有的订单为开奖了
      	Db::name('orders')->where('term', $qihao) ->update(['state'=>1]);
      	//对了加下钱
      	//终于结束了
	}


}